package com.oocl.euc.core.maintenance.repository;

import com.oocl.euc.core.builder.TeamBuilder;
import com.oocl.euc.core.builder.TeamEndorserBuilder;
import com.oocl.euc.core.builder.UsersBuilder;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.Team;
import com.oocl.euc.core.maintenance.model.TeamEndorser;
import com.oocl.euc.core.maintenance.model.Users;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TeamEndorserRepositoryIT extends ITTestBase {

    @Autowired
    UsersBuilder usersBuilder;

    @Autowired
    TeamBuilder teamBuilder;

    @Autowired
    TeamEndorserBuilder teamEndorserBuilder;

    @Autowired
    TeamRepository teamRepository;

    @Autowired
    TeamEndorserRepository teamEndorserRepository;

    @Autowired
    UsersRepository usersRepository;

    private List<String> usersIdList = new ArrayList<>();

    @After
    public void tearDown() throws Exception {
        teamEndorserRepository.deleteAll();
        teamRepository.deleteAll();
        for(String id : usersIdList) {
            usersRepository.deleteById(id);
        }
        usersIdList.clear();
    }

    @Test
    public void should_return_display_name_when_given_team_id() throws Exception {
        Users user = usersBuilder.defaultBuilder()
                .withStaffId("TEST USER STUFF ID")
                .withDisplayName("TEST USER DISPLAY NAME")
                .saveToDB();
        usersIdList.add(user.getStaffId());
        TeamEndorser teamEndorser = teamEndorserBuilder.defaultBuilder()
                .withUser(user)
                .saveToDB();
        Team team = teamBuilder.defaultBuilder()
//                .withTeamEndorsers(Arrays.asList(teamEndorser))
                .saveToDB();
        team.setTeamEndorsers(Arrays.asList(teamEndorser));
        teamRepository.save(team);
        List<String> displayNames = teamEndorserRepository.findDisplayName(team.getId());
        assertTrue(displayNames.size() > 0);
        assertEquals("TEST USER DISPLAY NAME", displayNames.get(0));
    }
}